package studyandroid.android.study.gophern.studyandroid.ui.listview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import studyandroid.android.study.gophern.studyandroid.R;

public class TestListView1Activity extends Activity implements AdapterView.OnItemLongClickListener, View.OnClickListener {


	private ListView mListView;
	private Button mDelBt;

	private BaseAdapter mBaseAdapter;

	private static final int CHOICE_MODE = 1;               //多选模式的标识
	private static final int DELETE_BUTTON_ID = 1;          //删除button的id
	private int mode = 0;                                   //当前模式的标识

	final String[] data = new String[]{"i", "like", "android"};//本例只做简单示范，不对数据进行删除操作。

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_list_view1);

		mListView = (ListView) findViewById(R.id.listview);
		mListView.setOnItemLongClickListener(this);

		initAdapter();                                                  //初始化adapter

		mListView.setAdapter(mBaseAdapter);
	}

	/**
	 * 初始化adapter。为方便大家看，这里就不继承重写BaseAdapter类了。
	 */
	private void initAdapter() {
		mBaseAdapter = new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {

					convertView = getLayoutInflater().inflate(R.layout.simple_item_listview, null);
				}

				TextView tv = (TextView) convertView.findViewById(R.id.simple_item_tv);
				tv.setText(data[position]);

				CheckBox cb = (CheckBox) convertView.findViewById(R.id.simple_item_cb);

				if (mode == CHOICE_MODE) {
					cb.setVisibility(View.VISIBLE);
				} else {
					cb.setVisibility(View.GONE);
				}
				return convertView;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public Object getItem(int position) {
				return data[position];
			}

			@Override
			public int getCount() {
				return data.length;
			}
		};
	}

	/*
	 * 监控listview的长按事件，当长按时更改视图，显示checkbox，让用户对
	 * 其进行多选。
	 */
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
								   int position, long id) {
		mode = CHOICE_MODE;
		((CheckBox) view.findViewById(R.id.simple_item_cb)).setChecked(true);    //选中被长按的item
		mBaseAdapter.notifyDataSetChanged();
		showDeleteButton();
		return false;
	}

	/**
	 * 展示删除按钮
	 */
	private void showDeleteButton() {
		android.widget.AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

		mDelBt = new Button(this);
		mDelBt.setId(DELETE_BUTTON_ID);
		mDelBt.setLayoutParams(params);
		mDelBt.setText("删除");
		mDelBt.setOnClickListener(this);
		mListView.addFooterView(mDelBt);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case DELETE_BUTTON_ID:
				//删除操作方法在这里，里面首先要获取哪些被选中，才能对其进行操作。
				delete();
				mListView.removeFooterView(mDelBt);     //隐藏删除button
				mode = 0;                   //还原为非多选模式，隐藏checkbox
				mBaseAdapter.notifyDataSetChanged();
				break;

			default:
				break;
		}
	}

	/**
	 * 删除item方法
	 */
	private void delete() {
		//可以是遍历哪个被check了，然后对其进行操作，数据方面需要有一个保存是否check的变量。本例为简便没有设置。
	}
}
